import { ADD_COUNTER, ADD_TOCART } from './mutations-type'

export default {
  addCart (context, payload) {
    // 1.查找之前数组中是否有该元素
    const oldProduct = context.state.cartList.find(item => item.id === payload.id)
    if (oldProduct) {
      context.commit(ADD_COUNTER, oldProduct)
    } else {
      payload.count = 1
      context.commit(ADD_TOCART, payload)
    }
    context.commit('saveToStorage')
  }
}
